package com.caoxw.Android.CityBus;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.telephony.TelephonyManager;

public class HttpUtil {
	static final String SERVER_ADDRESS = "http://www.cxw123.com/CityBus/"; // 结尾处要加"/"
	public boolean isError = false;
	public int	m_connTimeOut = 4000;
	public Exception err;

	public HttpUtil() {
		String phoneInfo = "Product: " + android.os.Build.PRODUCT;
		phoneInfo += "\r\nPU_ABI: " + android.os.Build.CPU_ABI;
		phoneInfo += "\r\nAGS: " + android.os.Build.TAGS;
		phoneInfo += "\r\nVERSION_CODES.BASE: "
				+ android.os.Build.VERSION_CODES.BASE;
		phoneInfo += "\r\nMODEL: " + android.os.Build.MODEL;
		phoneInfo += "\r\nSDK: " + android.os.Build.VERSION.SDK;
		phoneInfo += "\r\nVERSION.RELEASE: " + android.os.Build.VERSION.RELEASE;
		phoneInfo += "\r\nDEVICE: " + android.os.Build.DEVICE;
		phoneInfo += "\r\nDISPLAY: " + android.os.Build.DISPLAY;
		phoneInfo += "\r\nBRAND: " + android.os.Build.BRAND;
		phoneInfo += "\r\nBOARD: " + android.os.Build.BOARD;
		phoneInfo += "\r\nFINGERPRINT: " + android.os.Build.FINGERPRINT;
		phoneInfo += "\r\nID: " + android.os.Build.ID;
		phoneInfo += "\r\nMANUFACTURER: " + android.os.Build.MANUFACTURER;
		phoneInfo += "\r\nUSER: " + android.os.Build.USER;
		// Toast.makeText(this, phoneInfo, Toast.LENGTH_LONG).show();
		System.out.println(phoneInfo);
	}

	public String HttpGet(String url) {
		isError = false;
		String strResult = "";
		String strUrl = url.startsWith("http") ? url : (SERVER_ADDRESS + url);
		System.out.println(strUrl);
		HttpGet httpRequest = new HttpGet(strUrl);
		HttpParams httpParameters = new BasicHttpParams();// 
		HttpConnectionParams.setConnectionTimeout(httpParameters, m_connTimeOut);// Set the default socket timeout (SO_TIMEOUT) 
		HttpConnectionParams.setSoTimeout(httpParameters, m_connTimeOut+3000); 
		HttpClient client = new DefaultHttpClient(httpParameters);
		HttpResponse httpResponse;
		if(!isError){
			try {
				httpResponse = client.execute(httpRequest);
				int statusCode = httpResponse.getStatusLine().getStatusCode();
				if (statusCode != 200)
					return strResult;
				strResult = EntityUtils.toString(httpResponse.getEntity());
			} catch (Exception e) {
				isError = true;
				err = e;
			}
		}
		return strResult;
	}

	public String HttpPost(String url) {
		return HttpPost(url, null);
	}

	public String HttpPost(String url, HttpParams params) {
		isError = false;
		String strResult = "";
		String strUrl = url.startsWith("http") ? url : (SERVER_ADDRESS + url);
		System.out.println(strUrl);
		HttpPost httpRequest = new HttpPost(strUrl);
		if (null != params)
			httpRequest.setParams(params);
		HttpParams httpParameters = new BasicHttpParams();// 
		HttpConnectionParams.setConnectionTimeout(httpParameters, m_connTimeOut);// Set the default socket timeout (SO_TIMEOUT) 
		HttpConnectionParams.setSoTimeout(httpParameters, m_connTimeOut+3000); 
		HttpClient client = new DefaultHttpClient(httpParameters);
		HttpResponse httpResponse;
		try {
			httpResponse = client.execute(httpRequest);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode != 200)
				return strResult;
			strResult = EntityUtils.toString(httpResponse.getEntity());
		} catch (Exception e) {
			isError = true;
			err = e;
		}
		return strResult;
	}

	public int getLastestVersion() {
		String strContent = HttpGet(SERVER_ADDRESS + "/Version");
		int Result = 0;
		if(isError) return 0;
		try{
			Result = Integer.parseInt(strContent);
		}catch(NumberFormatException  e){
			isError = false;
			err = e;
		}
		return Result;
	}

	public void GetServerInfo() {
		HttpGet httpRequest = new HttpGet(SERVER_ADDRESS);
		HttpParams httpParameters = new BasicHttpParams();// 
		HttpConnectionParams.setConnectionTimeout(httpParameters, m_connTimeOut);// Set the default socket timeout (SO_TIMEOUT) 
		HttpConnectionParams.setSoTimeout(httpParameters, m_connTimeOut+3000); 
		HttpClient client = new DefaultHttpClient(httpParameters);
		HttpResponse httpResponse;

		try {
			httpResponse = client.execute(httpRequest);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			String strResult = EntityUtils.toString(httpResponse.getEntity());
			JSONObject jsonObject = new JSONObject(strResult);
			JSONArray arrJS = jsonObject.getJSONArray("DataList");

			JSONObject item = (JSONObject) arrJS.get(0);
			System.out.println(item.getString("name"));
			// System.out.println(jsonObject.get("LatestVersion").toString());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public ArrayList<CityDataItem> getDataList(String CityName) {
		isError = false;
		ArrayList<CityDataItem> Result = new ArrayList<CityDataItem>();
		String strUrl = (null==CityName || 0==CityName.length()) ? "CityData/getlist/name/" : "CityData/getlist/name/" + CityName;
		String strResult = HttpGet(strUrl);
		if (!isError) {
			try {
				JSONObject jsonObject = new JSONObject(strResult);
				JSONArray arrJS = jsonObject.getJSONArray("DataList");
				for (int i = 0; i < arrJS.length(); i++) {
					JSONObject objItem = arrJS.getJSONObject(i);
					CityDataItem Item = new CityDataItem();
					Item.Name = objItem.getString("name");
					Item.Id = objItem.getInt("id");
					Item.Size = objItem.getInt("size");
					Item.FileName = objItem.getString("dataFile");
					Item.UpdateTime = objItem.getString("updateTime");
					Result.add(Item);
					System.out.println(Item.Name);
				}

			} catch (JSONException e) {
				isError = true;
				e.printStackTrace();
			}
		}
		return Result;
	}

}
